package com.podium.web.log.service.impl;

import com.alibaba.fastjson2.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.podium.auth.entity.LoginUser;
import com.podium.comment.utils.StringUtils;
import com.podium.web.log.entity.SysLoginLog;
import com.podium.web.log.mapper.SysLoginLogMapper;
import com.podium.web.log.service.SysLoginLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 邵文泽
 * @description 登录日志
 * @date 2025-05-13 08:46:11
 */
@Slf4j
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {

    @Override
    public IPage<SysLoginLog> page(Page<SysLoginLog> page, SysLoginLog sysLoginLog) {
        QueryWrapper<SysLoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.hasLength(sysLoginLog.getUsername()), SysLoginLog::getUsername, sysLoginLog.getUsername())
                .like(StringUtils.hasLength(sysLoginLog.getNickName()), SysLoginLog::getNickName, sysLoginLog.getNickName())
                .likeRight(sysLoginLog.getLoginTime() != null, SysLoginLog::getLoginTime, DateUtils.format(sysLoginLog.getLoginTime(), "yyyy-MM-dd"))
                .eq(sysLoginLog.getLoginStatus() != null, SysLoginLog::getLoginStatus, sysLoginLog.getLoginStatus())
                .orderByDesc(SysLoginLog::getLoginTime)
        ;
        return super.page(page, queryWrapper);
    }

    @Override
    public List<SysLoginLog> list(SysLoginLog sysLoginLog) {
        QueryWrapper<SysLoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.hasLength(sysLoginLog.getUsername()), SysLoginLog::getUsername, sysLoginLog.getUsername())
                .like(StringUtils.hasLength(sysLoginLog.getNickName()), SysLoginLog::getNickName, sysLoginLog.getNickName())
                .likeRight(sysLoginLog.getLoginTime() != null, SysLoginLog::getLoginTime, DateUtils.format(sysLoginLog.getLoginTime(), "yyyy-MM-dd"))
                .eq(sysLoginLog.getLoginStatus() != null, SysLoginLog::getLoginStatus, sysLoginLog.getLoginStatus())
                .orderByDesc(SysLoginLog::getLoginTime)
        ;
        return super.list(queryWrapper);
    }

    @Override
    public SysLoginLog getById(Long id) {
        return super.getById(id);
    }

    @Override
    public boolean save(SysLoginLog sysLoginLog) {
        return super.save(sysLoginLog);
    }

    @Override
    @Async("logTaskExecutor")
    public void asyncSaveLog(SysLoginLog loginLog) {
        super.save(loginLog);
    }

    @Override
    public boolean updateById(SysLoginLog sysLoginLog) {
        return super.updateById(sysLoginLog);
    }

    @Override
    public void asyncEditLogoutTime(LoginUser loginUser) {
        SysLoginLog loginLog = super.getOne(new QueryWrapper<SysLoginLog>().lambda().eq(SysLoginLog::getToken, loginUser.getToken()));
        if (loginLog != null) {
            loginLog.setLogoutTime(new Date());
            super.updateById(loginLog);
        }
    }

    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }

}